package org.ishequ.domain;

import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import org.ishequ.db.NetConnectManager;
import android.content.Context;

public final class ConnectionState {
	
	public static ConnectionState instance = null;
	
	private Context context;
	
//	public static final String WEB_HOST = "http://192.168.1.215:8080/Jhost/";
	public static final String WEB_HOST = "http://do.jhost.cn/cfjiang/";
	
	private String sessionId = "";
	private boolean isConnected = false;
	private String connType = "";
	
	private ConnectionState(Context context) {
		this.context = context;
	}

	public synchronized static ConnectionState getInstance(Context context){
		if(instance == null) 
			instance = new ConnectionState(context);
		return instance;
	}
	
	public void connectionIni() {
		//判断网络连接状态
		if(NetConnectManager.isNetWorkAvailable(context)){
			setConnType(NetConnectManager.apnType());
			if((this.sessionId = webLogin())== ""){
				setConnected(false);
			}else{
				setConnected(true);
			}
		}else{
			setConnected(false);
		}
	}
	
	private String webLogin(){
		String sessionId="";
		try{
			String urlStr = WEB_HOST +"employee/login";
        	byte[] content = ("username="+URLEncoder.encode("admin1", "UTF-8")
								+"&password="+URLEncoder.encode("123", "UTF-8")).getBytes();
            URL u=new URL(urlStr);
            HttpURLConnection connection=(HttpURLConnection)u.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(false);
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("Content-Length", String.valueOf(content.length));
            connection.getOutputStream().write(content);
            if(connection.getResponseCode() == 302 
            		&& connection.getHeaderField("Location").split(";")[0].endsWith("control/center/main")){//跳转
            	String key = "";
                for (int i = 1; (key = connection.getHeaderFieldKey(i)) != null; i++) {
                    if (key.equalsIgnoreCase("set-cookie")) {
                         sessionId = connection.getHeaderField(key);
                         sessionId = sessionId.substring(0, sessionId.indexOf(";"));
                     }
                  }
            }else{//登录失败
            	sessionId="";
            }
		}catch(Exception e){
			e.printStackTrace();
		}
            return sessionId;
	}

	public boolean isConnected() {
		return isConnected;
	}

	public void setConnected(boolean isConnected) {
		this.isConnected = isConnected;
		if(!isConnected)this.connType = "";
	}

	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}

	public String getSessionId() {
		return sessionId;
	}

	public String getConnType() {
		return connType;
	}

	public void setConnType(String connType) {
		this.connType = connType;
		if("".equals(connType)) this.isConnected = false;
	}
}
